#include <genrand.h>

static inline int initGenerator()
{
    static int initflag = 0;
    if (!initflag)
    {
        srand(time(NULL) + clock());
    }
    initflag = 1;
    return 0;
}

static int generatorRandString_Common(char *outstr, int outsize, const char *genbuff, int gensize)
{
    // logdebug("need size=%d", outsize);
    if (outstr == NULL || outsize <= 0)
    {
        logerror("");
        return -1;
    }
    if (genbuff == NULL || gensize <= 0)
    {
        logerror("");
        return -1;
    }
    initGenerator();
    for (int i = 0; i < outsize; i++)
    {
        outstr[i] = genbuff[rand() % gensize];
    }
    return outsize;
}

int generatorRandString_Gigit(char *outstr, int outsize)
{
    static const char tmpbuffer[] = "0123456789";
    return generatorRandString_Common(outstr, outsize, tmpbuffer, sizeof(tmpbuffer) - 1);
}

int generatorRandString_Hex(char *outstr, int outsize)
{
    static const char tmpbuffer[] = "0123456789ABCDEF";
    return generatorRandString_Common(outstr, outsize, tmpbuffer, sizeof(tmpbuffer) - 1);
}

int generatorRandString_Aa1(char *outstr, int outsize)
{
    static const char tmpbuffer[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    return generatorRandString_Common(outstr, outsize, tmpbuffer, sizeof(tmpbuffer) - 1);
}

int generatorRandString_Aa1s(char *outstr, int outsize)
{
    static const char tmpbuffer[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz~!@#$^&*()_+{}:|<>?`;";
    return generatorRandString_Common(outstr, outsize, tmpbuffer, sizeof(tmpbuffer) - 1);
}
